package com.lk.theurlpro.service.impl;

import com.lk.theurlpro.dao.AllUrlsMapper;
import com.lk.theurlpro.pojo.Allurls;
import com.lk.theurlpro.service.AllurlsService;
import com.lk.theurlpro.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lk
 * @date 2020/2/5 0005 19:18
 */
@Service
public class AllurlsServiceImpl implements AllurlsService {

    @Autowired
    AllUrlsMapper allUrlsMapper;

    @Override
    public void save(Allurls allurls) {
        allurls.setDate(DateUtil.getNowDate());// 添加存档时间
        allUrlsMapper.save(allurls);
    }

    @Override
    public void sign(int u_id, int choice) {
        if(choice == 0)//恢复时（删除时，不更新时间）
            allUrlsMapper.updateTime(u_id,DateUtil.getNowDate());// 更新存档时间
        allUrlsMapper.sign(u_id, choice);
    }

    @Override
    public void update(Allurls allurls) {
        allurls.setDate(DateUtil.getNowDate());// 更新存档时间
        allUrlsMapper.update(allurls);
    }

    @Override
    public List<Allurls> findAll(int choice) {
        List<Allurls> list = allUrlsMapper.findAll(choice);
        return list;
    }

    @Override
    public Allurls get(int u_id) {
        Allurls allurls = allUrlsMapper.get(u_id);
        return allurls;
    }

    @Override
    public void delete(int u_id) {
        allUrlsMapper.delete(u_id);
    }

    @Override
    public List<Allurls> findByIndex(Allurls allurls, int choice) {
        List<Allurls> list = null;
        String u_title = allurls.getU_title();
        String type = allurls.getType();
        String date = allurls.getDate();
        list = allUrlsMapper.findAll(choice);
        if (u_title != "" | type != "" | date != ""){
            List<Allurls> listOfType = allUrlsMapper.findByType(type,choice);
            List<Allurls> listOfTitle = allUrlsMapper.findByTitle(u_title,choice);
            List<Allurls> listOfDate = allUrlsMapper.findByDate(date,choice);
            listOfTitle.retainAll(listOfType);
            listOfDate.retainAll(listOfTitle);
            list = listOfDate;
        }
        return list;
    }

}
